<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
</head>

<body>
    <script>
        // 模拟实现一个 localStorage
        // localStorage.setItem('ss',value)
        // localStorage.getItem('ss')
        // localStorage.removeItem('ss')
        let obj = {// 存到这里面
            cc: {},
            setItem: function (key, value) {
                this.cc[key] = value
            },
            getItem: function (key) {
                return this.cc[key] || null
            },
            removeItem: function (key) {
                delete this.cc[key]
            },
            clear() {
                this.cc = {}
            }
        }
        obj.setItem('ss', '1')
        obj.setItem('aa', 'aa')
        console.log(obj.getItem('ss'))
        console.log(obj.getItem('aa'))
        obj.removeItem('ss')
        console.log(obj.getItem('ss'))
        obj.clear()
        console.log(obj.getItem('aa'))

        // 第二种 
        const cd = (function () {
            let cc = {}       // 存到这里面
            return {
                setItem: (key, value) => cc[key] = value
                ,
                getItem: (key) => cc[key] || null,
                removeItem: (key) => delete cc[key],
                clear: () => cc = {}
   
            }
        })()
        Object.defineProperty(Object.prototype, 'my', {
            get() {
                return cd
            }
        })
        let ddd = '哇咔咔'
        let zzz = 'zzzzz'
        my.setItem('xx', ddd)
        my.setItem('zzz', zzz)
        console.log(my.getItem('xx'))
        my.removeItem('xx')
        console.log(my.getItem('xx'))
        console.log(my.getItem('zzz'))
        my.clear()
        console.log(my.getItem('zzz'))

    </script>
</body>

</html>